package com.heiyu.api.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.heiyu.api.request.GlobalIndexRequest;
import com.heiyu.api.response.GloalIndexResponse;
import com.heiyu.api.response.model.GlobalIndexModel;
import com.heiyu.api.response.model.MarketPriceInfo;
import com.heiyu.api.response.model.PointInfo;
import com.heiyu.controller.ApiBaseController;
import com.heiyu.dao.GlobalShowInfoMapper;
import com.heiyu.dao.ShowFull30minMapper;
import com.heiyu.entity.CoinBaseInfo;
import com.heiyu.entity.GlobalShowInfo;
import com.heiyu.entity.ShowFull30min;
import com.heiyu.utils.DateUtils;
import com.heiyu.utils.MoneyUtils;
import com.heiyu.utils.RangeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * Created by mountain on 18/6/11.
 */
@Api
@RestController
@RequestMapping("api/v1/global")
public class GobalController extends ApiBaseController{

    @Autowired
    private GlobalShowInfoMapper globalShowInfoMapper;
    @Autowired
    private ShowFull30minMapper showFull30minMapper;


    @ApiOperation(value="list",notes = "全球指数首页")
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public GloalIndexResponse list(@ModelAttribute GlobalIndexRequest request){
        GloalIndexResponse response = new GloalIndexResponse();
        response.success();
        GlobalIndexModel model = new GlobalIndexModel();
        GlobalShowInfo info = new GlobalShowInfo();
        info.setTargetId(request.getTargetId());
        info = globalShowInfoMapper.selectList(info).get(0);
        if(defaultSymbol()){
            model.setPrice(MoneyUtils.formatter(info.getPriceCny(),false));
            model.setDiffPirce(RangeUtil.diffPrice(info.getPriceCny(),info.getPercentChange24h()));
        }else{
            model.setPrice(MoneyUtils.formatter(info.getPriceUsd(),false));
            model.setDiffPirce(RangeUtil.diffPrice(info.getPriceUsd(),info.getPercentChange24h()));
        }

        model.setRange(RangeUtil.formatter(info.getPercentChange24h()));
        model.setExchangePirce(MoneyUtils.formatter(info.getPriceUsd(),false));
        model.setPointList(getKline(request.getTargetId(),request.getDayType()));
        model.setMarketPriceInfoList(Lists.newArrayList());
        CoinBaseInfo baseInfo = coinBaseInfoUtils.get(info.getUsName());
        if(baseInfo != null){
            model.setLogoUrl(baseInfo.getLogo());
        }
        JSONArray marketList = JSONArray.parseArray(redisUtils.get("REFER_COIN_MARKET_"+info.getSymbol()));
        if(marketList!=null&& marketList.size()>0){
            JSONObject obj;
            MarketPriceInfo priceInfo;
            for(int i=0;i<marketList.size();i++){
                obj = marketList.getJSONObject(i);
                priceInfo = new MarketPriceInfo();
                priceInfo.setPrice(MoneyUtils.formatter(new BigDecimal(obj.getDouble("price_cny")),false));
                priceInfo.setRank(RangeUtil.formatter(new BigDecimal(obj.getDouble("quality"))));
                priceInfo.setMarketName(obj.getString("marketName"));
                model.getMarketPriceInfoList().add(priceInfo);
            }
        }
        response.setData(model);
        return response;
    }

    private List<PointInfo> getKline(String usName, String type){
        List<PointInfo> pointInfoList = Lists.newArrayList();
        Map<String,Object> param = Maps.newHashMap();
        param.put("targetId",usName);
        List<ShowFull30min> list = null;
        PointInfo pointInfo;
        if(StringUtils.isEmpty(type)){
            type = "1";
        }
        switch (type){
            case "1":
                param.put("offset",48);
                list =  this.showFull30minMapper.selectForKline(param);
                if(list != null && list.size()>0){
                    for(ShowFull30min info:list){
                        pointInfo = new PointInfo();
                        pointInfo.setDateTime(DateUtils.format(info.getCreateTime(),"yyyy/MM/dd HH:mm"));
                        pointInfo.setPrice(MoneyUtils.formatter(defaultSymbol()?info.getPriceCny():info.getPriceUsd(),false));
                        pointInfo.setTimestamp(Integer.valueOf(info.getCreateTime().getTime()/1000+""));
                        pointInfoList.add(pointInfo);
                    }
                }
                break;
            case "2":
                param.put("offset",296);
                list =  this.showFull30minMapper.selectForKline(param);
                if(list != null && list.size()>0){
                    for(int i=0;i<list.size();i++){
                        if(i%8==0||list.size()-1==i){
                            pointInfo = new PointInfo();
                            pointInfo.setDateTime(DateUtils.format(list.get(i).getCreateTime(),"yyyy/MM/dd HH:mm"));
                            pointInfo.setPrice(MoneyUtils.formatter(defaultSymbol()?list.get(i).getPriceCny():list.get(i).getPriceUsd(),false));
                            pointInfo.setTimestamp(Integer.valueOf(list.get(i).getCreateTime().getTime()/1000+""));
                            pointInfoList.add(pointInfo);
                        }
                    }
                }
                break;
            case "3":
                param.put("offset",1176);
                list =  this.showFull30minMapper.selectForKline(param);
                if(list != null && list.size()>0){
                    for(int i=0;i<list.size();i++){
                        if(i%24==0||list.size()-1==i){
                            pointInfo = new PointInfo();
                            pointInfo.setDateTime(DateUtils.format(list.get(i).getCreateTime(),"yyyy/MM/dd HH:mm"));
                            pointInfo.setPrice(MoneyUtils.formatter(defaultSymbol()?list.get(i).getPriceCny():list.get(i).getPriceUsd(),false));
                            pointInfo.setTimestamp(Integer.valueOf(list.get(i).getCreateTime().getTime()/1000+""));
                            pointInfoList.add(pointInfo);
                        }
                    }
                }
                break;
            case "4":
                param.put("offset",14112);
                list =  this.showFull30minMapper.selectForKline(param);
                if(list != null && list.size()>0){
                    for(int i=0;i<list.size();i++){
                        if(i%296==0||list.size()-1==i){
                            pointInfo = new PointInfo();
                            pointInfo.setDateTime(DateUtils.format(list.get(i).getCreateTime(),"yyyy/MM/dd HH:mm"));
                            pointInfo.setPrice(MoneyUtils.formatter(defaultSymbol()?list.get(i).getPriceCny():list.get(i).getPriceUsd(),false));
                            pointInfo.setTimestamp(Integer.valueOf(list.get(i).getCreateTime().getTime()/1000+""));
                            pointInfoList.add(pointInfo);
                        }
                    }
                }
                break;
        }
        return Lists.reverse(pointInfoList);
    }

}
